*This file produces the results for Table 3, where we estimate the first-stage using the instruments which were selected through the LASSO procedure.

********************************************************************************
*DEFINE DIRECTORIES
local home CHILD
local main CHILD/JPE
local logs CHILD/JPE/logs
local data CHILD/JPE/data
local results CHILD/JPE/results
local network NETWORK
********************************************************************************

*generate log file
cap log close
local time = subinstr(c(current_date)," ","",.)
cd "`results'"
log using table3.txt, text replace

*bring in data
cd "`main'"
use regression_data_final_jpe.dta, clear

*merge in single treatment variable
preserve
use all_summary_statistics.dta, clear
keep patientid treated-badfda nd adhd
tempfile temp
save `temp',replace
restore

drop treat
drop drug
drop bad

merge 1:1 patientid using `temp'
keep if _merge==3
drop _merge

*get rid of extra variables
drop tca benzo badfda

*define drug treatment outcome
generate drugs = drug_only+both>0
drop drug_only therapy_only both

*merge in new bad prescribing variables
merge 1:1 patientid using bad_prescribing2.dta
foreach var of varlist fda_ok-red_flag {
	replace `var' = 0 if `var'==.
}
drop _merge

*reorder variables
order treated drugs red_flag grey_area fda_ok, after(patientid)

**incorporate ND diagnosis
drop adhd nd
cd "`home'"
merge 1:1 patientid using neurodevelopmental.dta
keep if _merge==3
drop _merge

*add hurting yourself outcome
merge 1:1 patientid using hurting_yourself.dta

*recode kids who don't have hurting yourself records; and hard code to 1's if more than 1
foreach var of varlist before-second_year {
	replace `var' = 0 if `var'==.
	replace `var' = 1 if `var'>1
}
drop _merge

*merge in REVISED DIAGNOSIS INFO
cd "`home'"
merge 1:1 patientid using diagnosis_revised_final.dta
keep if _merge==3
drop _merge

*save final sample
preserve
keep patientid zcta_kid year
save final_jpe_sample.dta, replace
restore

*merge in additional instrument
destring zcta_kid, replace
cd "`home'"
merge m:1 zcta_kid year using market-measures-antipsych.dta
drop if _merge==2
drop _merge

*look for which leave out measure needed
cd "`data'"
merge 1:1 patientid using status_of_leave_out.dta
drop _merge

generate share_ap = share_antipsych
replace share_ap = share_anti_yes if antipsychotic==1
replace share_ap = share_anti_no if antipsychotic==0

*define variables (for lasso/tsls)
local severity first_hospital first_er first_eval in_hospital_pre
local other female month year 
local diagnosis depression anxiety adjustment_anxiety adjustment_depression adjustment_dax  nd

*save ANY LASSO SELECTED instrument
local ins_all_treat  1.any_multiple#c.share_mh 1.any_multiple#c.s1_by_psych 1.any_multiple#c.share_ap 1.only_anxiety#c.s1_by_psych
local ins_treat_drug 1.first_eval#c.share_mh 1.first_eval#c.share_gp 1.anxiety#c.share_gp
local ins_drug_rf 1.any_depression#c.share_psych 1.only_adjustment#c.share_gp 1.only_adjustment#c.share_ap
local ins_drug_ga 1.any_depression#c.share_psych 1.any_depression#c.share_gp 1.any_depression#c.share_ap 1.any_multiple#c.share_mh 1.only_anxiety#c.share_gp 1.only_adjustment#c.share_gp
local ins_drug_fda  1.any_depression#c.share_mh

*change directory
cd "`results'"

eststo clear

*run first-stage regressions for branch 1 (treat or not)
local var treat

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis') `ins_all_treat', absorb(zip3) cluster(zip3)
quietly summarize `var' if e(sample)
estadd scalar Mean = r(mean)

****************************************
*run first-stage regressions for branch 1 (among treated)
keep if treated
****************************************

*effect of drug treatment
local var drugs

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis') `ins_treat_drug', absorb(zip3) cluster(zip3)
quietly summarize `var' if e(sample)
estadd scalar Mean = r(mean)

************************************************
*run first-stage regressions for branch 2 (among drug treated)
keep if drugs
************************************************

*effect of red-flag drug treatment
local var red_flag

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis') `ins_drug_rf', absorb(zip3) cluster(zip3)
quietly summarize `var' if e(sample)
estadd scalar Mean = r(mean)

*effect of grey-area drug treatment
local var grey_area

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis') `ins_drug_ga', absorb(zip3) cluster(zip3)
quietly summarize `var' if e(sample)
estadd scalar Mean = r(mean)

*effect of fda-ok drug treatment
local var fda_ok

eststo:   quietly areg `var' i.(`other') i.age i.(`severity') i.(`diagnosis') `ins_drug_fda', absorb(zip3) cluster(zip3)
quietly summarize `var' if e(sample)
estadd scalar Mean = r(mean)

esttab using table3.csv, star(+ 0.10 ** 0.05 *** 0.01) drop(*month* *year*) order(1.any_multiple#c.s1_by_psych  1.only_anxiety#c.s1_by_psych  1.first_eval#c.share_gp 1.any_depression#c.share_gp 1.only_adjustment#c.share_gp 1.anxiety#c.share_gp  1.only_adjustment#c.share_gp  1.only_anxiety#c.share_gp 1.any_depression#c.share_psych 1.first_eval#c.share_mh 1.any_multiple#c.share_mh  1.any_depression#c.share_mh 1.any_multiple#c.share_ap 1.any_depression#c.share_ap    1.only_adjustment#c.share_ap )   stats(N r2 Mean, fmt(0 %8.3f %8.3f)) replace se mtitles("Treated" "Drugs" "Red-Flag" "Grey-Area" "FDA-Approved") nonumber title("First-stage with LASSO Selected Instruments") label nobase noabbrev  nogap b(%8.3f) se(%8.3f) 

cap log close